package com.tpsoft.sys.service.impl;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.tpsoft.controller.CurrentUserControllerAdvice;
import com.tpsoft.sys.bean.UrlRolesBean;
import com.tpsoft.sys.domain.Role;
import com.tpsoft.sys.domain.User;
import com.tpsoft.sys.repository.RoleRepository;
import com.tpsoft.sys.repository.UserRepository;
import com.tpsoft.sys.repository.UserRoleRepository;
import com.tpsoft.sys.service.SysServiceI;
import com.tpsoft.sys.service.UrlCache;
@Service
@Transactional(readOnly=true)
public class SysServiceImpl  implements SysServiceI{
    private static final Logger logger = LoggerFactory.getLogger(CurrentUserControllerAdvice.class);

	@Autowired
	private UserRepository userRepository;
	@Autowired
	private UserRoleRepository userRoleRepository;
	@Autowired
	private RoleRepository roleRepository;
	@Autowired
	private UrlCache urlCache;
	
	public User getUser(String username){
		return userRepository.getByUsername(username);	
	}
	
	public void getUrlRoles(){
		List<Object[]> roleActions=roleRepository.getUrlRoles();
		List<UrlRolesBean> list=Lists.newArrayList();
		for (Object[] obj: roleActions) {
			UrlRolesBean b=new UrlRolesBean();
			b.setUrl(obj[0].toString());
			b.setRole(obj[1].toString());
			logger.debug("Role Name "+b.getRole());
			logger.debug("Action Name "+b.getUrl());
			list.add(b);
		}
		urlCache.mapUrlToRole(list);
	return ;
	}

	@Override
	public List<Role> listRoles() {
		return roleRepository.findAll();
	}
	
}
